package com.shopnight.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopnight.constant.ShopIndentConstant;
import com.shopnight.entity.AllOrder;
import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.service.AllOrderService;
import com.shopnight.mapper.AllOrderMapper;
import com.shopnight.service.ErrandOrderService;
import com.shopnight.service.ShopIndentService;
import com.shopnight.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 13201
* @description 针对表【all_order】的数据库操作Service实现
* @createDate 2023-04-11 16:53:46
*/
@Slf4j
@Service
public class AllOrderServiceImpl extends ServiceImpl<AllOrderMapper, AllOrder>
    implements AllOrderService{
   @Autowired
    AllOrderMapper allOrderMapper;
   @Autowired
   ErrandOrderService errandOrderService;

    @Override
    public  List<JSONObject> getAllOrder(Integer orgId) {
        List<AllOrder> allOrders = allOrderMapper.selectList(
                new LambdaQueryWrapper<AllOrder>()
                        .eq(AllOrder::getOrgId, orgId)
                        .eq(AllOrder::getOrderStatus, ShopIndentConstant.StatusEnum.underway.getStatus())
                        .ne(AllOrder::getOrderType, ShopIndentConstant.orderTypeEnum.shop.getTyep())
        );

        List<Object> Records = new ArrayList<Object>();
        List<Integer> errandIds = new ArrayList<Integer>();


        allOrders.stream().forEach(order->{
            switch ( order.getOrderType() ) {
                case 2:   // 处理跑腿订单
                    errandIds.add(order.getOrderId());
                    break;
                default:
                    log.info("未知的支付类型");
                    break;
            }
        });

        //获取跑腿订单数据
        if (errandIds.size() > 0 )  errandOrderService.getRiderErandOrderList(Records,errandIds);


        List<JSONObject> collect = Records.stream().map(item -> {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(item);
                    return jsonObject;
                }).sorted((item1,item2)->{
                    return  (((Date)item2.get("createTime")).getTime() - ((Date)item1.get("createTime")).getTime()) > 0 ? 1 : -1;
                })
                .collect(Collectors.toList());


        return collect;
    }

    @Override
    public List<JSONObject> getRiderReceivingOrder( Integer orgId,Integer status) {
        //首先读取用户id 防止瞬间用户id被覆盖
        Integer userId = Integer.valueOf(UserContext.getEndUserId());

        List<AllOrder> allOrders = allOrderMapper.selectList(
                new LambdaQueryWrapper<AllOrder>()
                        .eq(AllOrder::getOrgId, orgId)
                        .eq(AllOrder::getReceivingUserId, userId)
                        .eq(AllOrder::getOrderStatus, status)
                        .ne(AllOrder::getOrderType, ShopIndentConstant.orderTypeEnum.shop.getTyep())
        );

        List<Object> Records = new ArrayList<Object>();
        List<Integer> errandIds = new ArrayList<Integer>();


        allOrders.stream().forEach(order->{
            switch ( order.getOrderType() ) {
                case 2:   // 处理跑腿订单
                    errandIds.add(order.getOrderId());
                    break;
                default:
                    log.info("未知的支付类型");
                    break;
            }
        });

        //获取跑腿订单数据
        if (errandIds.size() > 0 )  errandOrderService.getReceivingErandOrderList(Records,errandIds);


        List<JSONObject> collect = Records.stream().map(item -> {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(item);
                    return jsonObject;
                }).sorted((item1,item2)->{
                    return  (((Date)item2.get("createTime")).getTime() - ((Date)item1.get("createTime")).getTime()) > 0 ? 1 : -1;
                })
                .collect(Collectors.toList());


        return collect;
    }



}




